package com.atguigu.educms.controller;


import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.atguigu.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-14
 */
//后台banner管理接口
@RestController
@RequestMapping("/cmsservice/banneradmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService crmBannerService;

    //查询所有轮播图
    @GetMapping("/getAllBanner")
    public R getAllBanner() {
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        List<CrmBanner> list = crmBannerService.list(wrapper);
        return R.ok().data("list", list);
    }

    //分页查询
    @GetMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable Long page, @PathVariable Long limit) {
        Page<CrmBanner> pageBanner = new Page<>(page, limit);
        IPage<CrmBanner> page1 = crmBannerService.page(pageBanner, null);
        long total = page1.getTotal();
        List<CrmBanner> records = page1.getRecords();
        return R.ok().data("total", total).data("records", records);
    }

    //添加轮播图
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        crmBannerService.save(crmBanner);
        return R.ok();
    }

    //根据id获取轮播图
    @GetMapping("/get/{id}")
    public R get(@PathVariable String id) {
        crmBannerService.getById(id);
        return R.ok();
    }

    //更新轮播图
    @PutMapping("/update")
    public R update(@RequestBody CrmBanner crmBanner) {
        crmBannerService.updateById(crmBanner);
        return R.ok();
    }

    //删除轮播图
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable String id) {
        crmBannerService.removeById(id);
        return R.ok();
    }

}

